var express = require('express')
var session = require('express-session');  //session的持久化模块
var bodyParser = require('body-parser');
var router = require('./routes/index') //路由信息
var config = require('./config/config.base') //基础信息
var app = express();



app.use(express.static('./static'));  //负责托管静态资源


app.use(bodyParser.json());	 // 解析 application/json
app.use(bodyParser.urlencoded({ extended: false })); //解析 application/x-www-form-urlencoded

app.use(session({
	secret: 'secret', //为了安全性的考虑设置secret属性
	cookie: {maxAge: 60 * 1000 * 30}, //设置过期时间
	resave: true, // 即使 session 没有被修改，也保存 session 值，默认为 true
	saveUninitialized: false, //
}));

//执行全局路由信息配置
app.all('*', (req, res, next) => {
	res.header("Access-Control-Allow-Origin", '*');
	res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  	res.header("Access-Control-Allow-Credentials", true); //可以带cookies
	res.header("X-Powered-By", '3.2.1')
	if (req.method == 'OPTIONS') {
	  	res.send(200);
	} else {
	    next();
	}
});

/**app.use(function (err, req, res, next) {
	console.log(err);
	res.json({status:1 ,msg:err})
});**/

router(app);

var server = app.listen(config.port, function () {
    
     var host = server.address().address
     var port = server.address().port
     console.log("应用实例，访问地址为 http://%s:%s", host, port)    
})